﻿using Ceri.Platform.EAM_NEW.API.Common.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Ceri.Platform.EAM_NEW.API.Services.BASE
{
    public interface IBaseServices<TEntity> where TEntity : class
    {
        int GetSequenceNextVal(string seqName);

        #region 异步

        Task<TEntity> QueryByIdAsync(object objId);
        Task<TEntity> QueryByIdAsync(object objId, bool blnUseCache = false);
        Task<List<TEntity>> QueryByIDsAsync(object[] lstIds);

        Task<int> AddAsync(TEntity model);

        Task<int> AddAsync(List<TEntity> listEntity);

        Task<bool> DeleteByIdAsync(object id);

        Task<bool> DeleteAsync(TEntity model);

        Task<bool> DeleteByIdsAsync(object[] ids);

        Task<bool> RemoveAsync(Expression<Func<TEntity, bool>> whereExpression);

        Task<bool> UpdateAsync(TEntity model);
        Task<bool> UpdateAsync(TEntity entity, string where);

        Task<bool> UpdateAsync(object operateAnonymousObjects);

        Task<bool> UpdateAsync(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "");

        Task<List<TEntity>> QueryAsync();
        Task<List<TEntity>> QueryAsync(string where);
        Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression);
        Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
        Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression);
        Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
        Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
        Task<List<TEntity>> QueryAsync(string where, string orderByFields);
        Task<List<TEntity>> QuerySqlAsync(string sql, SugarParameter[] parameters = null);
        Task<DataTable> QueryTableAsync(string sql, SugarParameter[] parameters = null);

        Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields);
        Task<List<TEntity>> QueryAsync(string where, int top, string orderByFields);

        Task<List<TEntity>> QueryAsync(
            Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields);
        Task<List<TEntity>> QueryAsync(string where, int pageIndex, int pageSize, string orderByFields);


        Task<PageModel<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFields = null);
        //Task<PageModel<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFields = null);
        Task<List<TResult>> QueryMuchAsync<T, T2, T3, TResult>(
            Expression<Func<T, T2, T3, object[]>> joinExpression,
            Expression<Func<T, T2, T3, TResult>> selectExpression,
            Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new();

        #endregion 异步

        #region  同步

        TEntity QueryById(object objId);
        TEntity QueryById(object objId, bool blnUseCache = false);
        List<TEntity> QueryByIDs(object[] lstIds);

        int Add(TEntity model);

        int Add(List<TEntity> listEntity);

        bool DeleteById(object id);

        bool Delete(TEntity model);

        bool DeleteByIds(object[] ids);

        bool Remove(Expression<Func<TEntity, bool>> whereExpression);

        bool Update(TEntity model);
        bool Update(TEntity entity, string where);

        bool Update(object operateAnonymousObjects);

        bool Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "");

        List<TEntity> Query();
        List<TEntity> Query(string where);
        List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression);
        List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
        List<TResult> Query<TResult>(Expression<Func<TEntity, TResult>> expression);
        List<TResult> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
        List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
        List<TEntity> Query(string where, string orderByFields);
        List<TEntity> QuerySql(string sql, SugarParameter[] parameters = null);
        DataTable QueryTable(string sql, SugarParameter[] parameters = null);

        List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields);
        List<TEntity> Query(string where, int top, string orderByFields);

        List<TEntity> Query(
            Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields);
        List<TEntity> Query(string where, int pageIndex, int pageSize, string orderByFields);


        PageModel<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFields = null);

        List<TResult> QueryMuch<T, T2, T3, TResult>(
            Expression<Func<T, T2, T3, object[]>> joinExpression,
            Expression<Func<T, T2, T3, TResult>> selectExpression,
            Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new();

        #endregion  同步
    }

}
